/**
  ******************************************************************************
  * @file    py32f0xx_it.c
  * @author  MCU Application Team
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by Puya under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "py32f0xx_it.h"

/* Private includes ----------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* External variables --------------------------------------------------------*/

/******************************************************************************/
/*           Cortex-M0+ Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  while (1)
  {
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  app_sys_ticks += 1;
}

/******************************************************************************/
/* PY32F0xx Peripheral Interrupt Handlers                                     */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file.                                          */
/******************************************************************************/
void EXTI4_15_IRQHandler(void)
{
  /* Handle EXTI interrupt request */
  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_4))
  {
    LL_EXTI_ClearFlag(LL_EXTI_LINE_4);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_5))  //SW ENTER 
  {
		drv_btn_irq_handler(BTN_1);
    LL_EXTI_ClearFlag(LL_EXTI_LINE_5);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_6)) //angle sw
	{
    irq_callback_angle_sw();
    // static unsigned char temp = 0;
    // temp +=1;
    // if (temp >= 2)
    // {
    //     irq_callback_angle_sw();
    //     temp = 0;
    // }
    LL_EXTI_ClearFlag(LL_EXTI_LINE_6);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_7))  //DC 
  {
    app_power_irq_handler();
    LL_EXTI_ClearFlag(LL_EXTI_LINE_7);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_8))  //SW A
  {
    drv_btn_irq_handler(BTN_A);
    LL_EXTI_ClearFlag(LL_EXTI_LINE_8);
  } 
}

void EXTI0_1_IRQHandler(void)
{
  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_0))
  {
    //drv_btn_irq_handler(BTN_4);
    LL_EXTI_ClearFlag(LL_EXTI_LINE_0);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_1)) {
    //drv_btn_irq_handler(BTN_3);
    LL_EXTI_ClearFlag(LL_EXTI_LINE_1);
  }
}

void EXTI2_3_IRQHandler(void)
{
  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_2))  
  {
    LL_EXTI_ClearFlag(LL_EXTI_LINE_2);
  }

  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_3)) {
    LL_EXTI_ClearFlag(LL_EXTI_LINE_3);
  }
}

//void TIM3_IRQHandler(void)
//{
//  if(LL_TIM_IsActiveFlag_UPDATE(TIM3) && LL_TIM_IsEnabledIT_UPDATE(TIM3))
//  {
//    app_record_tim_irq_handler();
//    LL_TIM_ClearFlag_UPDATE(TIM3);
//  }
//}

//void TIM14_IRQHandler(void)
//{
//  if(LL_TIM_IsActiveFlag_UPDATE(TIM14) && LL_TIM_IsEnabledIT_UPDATE(TIM14))
//  {
//    // app_motor_irq_handler();
//    LL_TIM_ClearFlag_UPDATE(TIM14);
//  }
//}

void TIM16_IRQHandler(void) //LED Init
{
  if(LL_TIM_IsActiveFlag_UPDATE(TIM16) && LL_TIM_IsEnabledIT_UPDATE(TIM16))
  {
    app_led_timer_irq_handler();
    LL_TIM_ClearFlag_UPDATE(TIM16);
  }
}

void TIM17_IRQHandler(void) //buzzer
{
	if(LL_TIM_IsActiveFlag_UPDATE(TIM17))
  //if(LL_TIM_IsActiveFlag_UPDATE(TIM17) && LL_TIM_IsEnabledIT_UPDATE(TIM17))
  {
		app_buzzer_pwm_irq_handler();
    LL_TIM_ClearFlag_UPDATE(TIM17);
  }
}

//void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
//{
//  if(LL_TIM_IsActiveFlag_UPDATE(TIM1) && LL_TIM_IsEnabledIT_UPDATE(TIM1))
//  {
//    LL_TIM_ClearFlag_UPDATE(TIM1);
//  }
//}

//void USART2_IRQHandler(void)
//{
//}

void USART1_IRQHandler(void)
{
  drv_uart_irq_handler(UART_USART1);
}

//void RTC_IRQHandler(void)
//{
//  if ((LL_RTC_IsActiveFlag_ALR(RTC) != 0)&& (LL_RTC_IsEnabledIT_ALR(RTC) != 0))
//  {
//    /* Clear alarm flag */
//    LL_RTC_ClearFlag_ALR(RTC);
//  }
//}

void LPTIM1_IRQHandler(void)
{
  if(LL_LPTIM_IsActiveFlag_ARRM(LPTIM) == 1)
  {
    LL_LPTIM_ClearFLAG_ARRM(LPTIM);
  }
}


/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
